package com.bytecodes.eaulas.mantenimiento;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.rmi.RemoteException;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.LayoutStyle;
import javax.swing.SwingConstants;

import com.bytecodes.eaulas.comun.EAulasTableModel;
import com.bytecodes.eaulas.comun.Utils;
import com.bytecodes.eaulas.comun.servicios.EAulasServiciosImpl;
import com.bytecodes.eaulas.comun.servicios.EAulasServiciosInterface;
import com.bytecodes.eaulas.comun.servicios.TDSLanguageUtils;


/**
 * @author David Fern�ndez G�mez
 */
public class PanelBajaAulas extends JPanel {
	private EAulasServiciosInterface servicios ;
	private String idCliente=null;
	public PanelBajaAulas(String idCliente) {
		this.idCliente= idCliente;
		try {
			servicios = new EAulasServiciosImpl();
		} catch (RemoteException exception) {
			exception.printStackTrace();
		}
		initComponents();
	}

	private void buttonAnadirActionPerformed(ActionEvent e) {
		// TODO add your code here
	}

	private void buttonCancelActionPerformed(ActionEvent e) {
		// TODO add your code here
	}

	private void buttonBuscarActionPerformed(ActionEvent e) {
		String descripcion =this.textFieldDescripcion.getText();
		try {
			String filtro = null;
			if(!"".equals(descripcion)){
				 filtro = " WHERE spacedescription like '%"+descripcion+"%'";
				 tableModel= servicios.getTableModel("spaces", filtro);
				 tableModel.fireTableDataChanged();
				 tableResultados.setModel(tableModel);
			}
		} catch (RemoteException exception) {
			System.out.println("Error");
		}
		
	}

	private void buttonEliminarActionPerformed(ActionEvent e) {
		if(!"".equals(textFieldIdentificador.getText())){
			try {
				int result = JOptionPane.showConfirmDialog(null, TDSLanguageUtils.getMessage("panelGestionAula.borrar.pregunta"),
						 TDSLanguageUtils.getMessage("panelGestionAula.borrar"), JOptionPane.OK_CANCEL_OPTION);
				if(result==0){
					Integer idAula=new Integer(textFieldIdentificador.getText());
					servicios.BajaAula(idAula, idCliente);
					JOptionPane.showMessageDialog(null, TDSLanguageUtils
							.getMessage("panelGestionAula.borrar.ok"),
							TDSLanguageUtils.getMessage("panelGestionAula.borrar"),
							JOptionPane.INFORMATION_MESSAGE);
					Utils.clearPanel(this);
					tableModel.fireTableDataChanged();
					tableResultados.setModel(tableModel);
				}
			} catch (RemoteException exception) {
				exception.printStackTrace();
			}
		}
		else{
			JOptionPane.showMessageDialog(null, TDSLanguageUtils
					.getMessage("panelGestionAula.borrar.ko"),
					TDSLanguageUtils.getMessage("panelGestionAula.aula"),
					JOptionPane.ERROR_MESSAGE);
		}
	}

	private void initComponents() {
		label1 = new JLabel();
		label2 = new JLabel();
		label3 = new JLabel();
		label4 = new JLabel();
		label5 = new JLabel();
		label6 = new JLabel();
		label7 = new JLabel();
		label8 = new JLabel();
		label9 = new JLabel();
		label10 = new JLabel();
		textFieldIdentificador = new JTextField();
		textFieldDescripcion = new JTextField();
		textFieldCapacidad = new JTextField();
		textFieldPrecioHora = new JTextField();
		textFieldPrecioDia = new JTextField();
		textFieldURL = new JTextField();
		textPaneIndicaciones = new JTextPane();
		textFieldDireccion = new JTextField();
		textFieldCiudad = new JTextField();
		textFieldCodigoPostal = new JTextField();
		buttonEliminar = new JButton();
		buttonCancel = new JButton();
		buttonBuscar = new JButton();
		try {
			tableModel= servicios.getTableModel("spaces"," where inactivation_date is null");
		} catch (RemoteException exception1) {
			exception1.printStackTrace();
		}
		tableResultados = new JTable(tableModel);
		tableResultados.addMouseListener(new MouseAdapter() 
		   {
		      public void mouseClicked(MouseEvent e) 
		      {
		         int fila = tableResultados.rowAtPoint(e.getPoint());
		        // int columna = tableResultados.columnAtPoint(e.getPoint());
		         rellenarDatos( fila );
		      }

			
		   });


		//---- label1 ----
		label1.setText(TDSLanguageUtils.getMessage("panelGestionAula.aula"));

		//---- label2 ----
		label2.setText(TDSLanguageUtils.getMessage("panelGestionAula.descripcion"));

		//---- label3 ----
		label3.setText(TDSLanguageUtils.getMessage("panelGestionAula.capacidad"));

		//---- label4 ----
		label4.setText(TDSLanguageUtils.getMessage("panelGestionAula.precioHora"));

		//---- label5 ----
		label5.setText(TDSLanguageUtils.getMessage("panelGestionAula.precioDia"));

		//---- label6 ----
		label6.setText(TDSLanguageUtils.getMessage("panelGestionAula.url"));

		//---- label7 ----
		label7.setText(TDSLanguageUtils.getMessage("panelGestionAula.indicaciones"));

		//---- label8 ----
		label8.setText(TDSLanguageUtils.getMessage("panelGestionAula.codigoPostal"));

		//---- label9 ----
		label9.setText(TDSLanguageUtils.getMessage("panelGestionAula.ciudad"));

		//---- label10 ----
		label10.setText(TDSLanguageUtils.getMessage("panelGestionAula.direccion"));

		//---- buttonEliminar ----
		buttonEliminar.setText(TDSLanguageUtils.getMessage("panelGestionAula.eliminar"));
		buttonEliminar.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				buttonAnadirActionPerformed(e);
				buttonEliminarActionPerformed(e);
			}
		});

		//---- buttonCancel ----
		buttonCancel.setText(TDSLanguageUtils.getMessage("panelGestionAula.cancelar"));
		buttonCancel.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				buttonCancelActionPerformed(e);
				buttonCancelActionPerformed(e);
			}
		});

		//---- buttonBuscar ----
		buttonBuscar.setText(TDSLanguageUtils.getMessage("panelGestionAula.buscar"));
		buttonBuscar.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				buttonBuscarActionPerformed(e);
			}
		});

		GroupLayout layout = new GroupLayout(this);
		setLayout(layout);
		layout.setHorizontalGroup(
				layout.createParallelGroup()
				.addGroup(layout.createSequentialGroup()
						.addGap(157, 157, 157)
						.addComponent(buttonBuscar)
						.addGap(18, 18, 18)
						.addComponent(buttonEliminar)
						.addGap(18, 18, 18)
						.addComponent(buttonCancel)
						.addContainerGap(172, Short.MAX_VALUE))
						.addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(layout.createParallelGroup()
										.addComponent(label1)
										.addComponent(label2)
										.addComponent(label3)
										.addComponent(label4)
										.addComponent(label5)
										.addComponent(label6)
										.addComponent(label7)
										.addComponent(label10)
										.addComponent(label9))
										.addGap(34, 34, 34)
										.addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
												.addComponent(tableResultados, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 700, Short.MAX_VALUE)
												.addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
														.addComponent(textFieldCiudad, GroupLayout.PREFERRED_SIZE, 184, GroupLayout.PREFERRED_SIZE)
														.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 98, Short.MAX_VALUE)
														.addComponent(label8)
														.addGap(18, 18, 18)
														.addComponent(textFieldCodigoPostal, GroupLayout.PREFERRED_SIZE, 81, GroupLayout.PREFERRED_SIZE))
														.addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
																.addComponent(textFieldIdentificador, GroupLayout.PREFERRED_SIZE, 111, GroupLayout.PREFERRED_SIZE)
																.addGap(335, 335, 335))
																.addComponent(textFieldDescripcion, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 446, Short.MAX_VALUE)
																.addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
																		.addComponent(textFieldCapacidad, GroupLayout.PREFERRED_SIZE, 141, GroupLayout.PREFERRED_SIZE)
																		.addGap(305, 305, 305))
																		.addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
																				.addComponent(textFieldPrecioHora, GroupLayout.PREFERRED_SIZE, 131, GroupLayout.PREFERRED_SIZE)
																				.addGap(305, 305, 305))
																				.addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
																						.addComponent(textFieldPrecioDia, GroupLayout.DEFAULT_SIZE, 141, Short.MAX_VALUE)
																						.addGap(305, 305, 305))
																						.addComponent(textFieldURL, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 446, Short.MAX_VALUE)
																						.addComponent(textPaneIndicaciones, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 446, Short.MAX_VALUE)
																						.addComponent(textFieldDireccion, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 446, Short.MAX_VALUE))
																						.addGap(56, 56, 56))
		);
		layout.linkSize(SwingConstants.HORIZONTAL, new Component[] {textFieldCapacidad, textFieldPrecioDia, textFieldPrecioHora});
		layout.linkSize(SwingConstants.HORIZONTAL, new Component[] {buttonBuscar, buttonCancel, buttonEliminar});
		layout.setVerticalGroup(
				layout.createParallelGroup()
				.addGroup(layout.createSequentialGroup()
						.addGap(19, 19, 19)
						.addGroup(layout.createParallelGroup()
								.addComponent(label1)
								.addComponent(textFieldIdentificador, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
								.addGap(18, 18, 18)
								.addGroup(layout.createParallelGroup()
										.addComponent(label2)
										.addComponent(textFieldDescripcion, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
										.addGap(18, 18, 18)
										.addGroup(layout.createParallelGroup()
												.addComponent(label3)
												.addComponent(textFieldCapacidad, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
												.addGap(18, 18, 18)
												.addGroup(layout.createParallelGroup()
														.addComponent(label4)
														.addComponent(textFieldPrecioHora, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
														.addGap(18, 18, 18)
														.addGroup(layout.createParallelGroup()
																.addComponent(label5)
																.addComponent(textFieldPrecioDia, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
																.addGap(18, 18, 18)
																.addGroup(layout.createParallelGroup()
																		.addComponent(label6)
																		.addComponent(textFieldURL, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
																		.addGap(18, 18, 18)
																		.addGroup(layout.createParallelGroup()
																				.addGroup(layout.createSequentialGroup()
																						.addComponent(label7)
																						.addGap(75, 75, 75)
																						.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
																								.addComponent(label10)
																								.addComponent(textFieldDireccion, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
																								.addComponent(textPaneIndicaciones, GroupLayout.PREFERRED_SIZE, 76, GroupLayout.PREFERRED_SIZE))
																								.addGap(18, 18, 18)
																								.addGroup(layout.createParallelGroup()
																										.addComponent(label9)
																										.addComponent(textFieldCiudad, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
																										.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
																												.addComponent(label8)
																												.addComponent(textFieldCodigoPostal, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
																												.addGap(30, 30, 30)
																												.addComponent(tableResultados, GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE)
																												.addGap(18, 18, 18)
																												.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
																														.addComponent(buttonEliminar)
																														.addComponent(buttonBuscar)
																														.addComponent(buttonCancel))
																														.addGap(30, 30, 30))
		);
		textFieldIdentificador.setEditable(false);
		textFieldIdentificador.setEnabled(false);
	}

	protected void rellenarDatos(int fila) {
		Utils.clearPanel(this);
		if(tableModel.getValueAt(fila,2)!=null)
			this.textFieldCapacidad.setText(tableModel.getValueAt(fila,2).toString());
		if(tableModel.getValueAt(fila,8)!=null)
			this.textFieldCiudad.setText(tableModel.getValueAt(fila,8).toString());
		if(tableModel.getValueAt(fila,7)!=null)
			this.textFieldCodigoPostal.setText(tableModel.getValueAt(fila,7).toString());
		if(tableModel.getValueAt(fila,1)!=null)
			this.textFieldDescripcion.setText(tableModel.getValueAt(fila,1).toString());
		if(tableModel.getValueAt(fila,9)!=null)
			this.textFieldDireccion.setText(tableModel.getValueAt(fila,9).toString());
		if(tableModel.getValueAt(fila,0)!=null)
			this.textFieldIdentificador.setText(tableModel.getValueAt(fila,0).toString());
		if(tableModel.getValueAt(fila,3)!=null)
			this.textFieldPrecioDia.setText(tableModel.getValueAt(fila,3).toString());
		if(tableModel.getValueAt(fila,4)!=null)
			this.textFieldPrecioHora.setText(tableModel.getValueAt(fila,4).toString());
		if(tableModel.getValueAt(fila,5)!=null)
			this.textFieldURL.setText(tableModel.getValueAt(fila,5).toString());
		if(tableModel.getValueAt(fila,6)!=null)
			this.textPaneIndicaciones.setText(tableModel.getValueAt(fila,6).toString());
	}


	private JLabel label1;
	private JLabel label2;
	private JLabel label3;
	private JLabel label4;
	private JLabel label5;
	private JLabel label6;
	private JLabel label7;
	private JLabel label8;
	private JLabel label9;
	private JLabel label10;
	private JTextField textFieldIdentificador;
	private JTextField textFieldDescripcion;
	private JTextField textFieldCapacidad;
	private JTextField textFieldPrecioHora;
	private JTextField textFieldPrecioDia;
	private JTextField textFieldURL;
	private JTextPane textPaneIndicaciones;
	private JTextField textFieldDireccion;
	private JTextField textFieldCiudad;
	private JTextField textFieldCodigoPostal;
	private JButton buttonEliminar;
	private JButton buttonCancel;
	private JButton buttonBuscar;
	private JTable tableResultados;
	private EAulasTableModel tableModel;
}
